package com.scpii.universal.struts.manage;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.scpii.universal.action.BaseAction;
import com.scpii.universal.dto.AppApiDto;
import com.scpii.universal.dto.AppApiResultDto;
import com.scpii.universal.service.CreateAppService;
import com.scpii.universal.util.CacheUtil;

@Controller
@Scope("prototype")
public class AutoCreateAppApiAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 8279115881208038765L;

	@Resource
	public CreateAppService createAppService;
	// 传入参数
	public AppApiDto appApiDto;

	private List<AppApiResultDto> resultlist;

	private String jsonData;

	/**
	 * 自动创建app
	 * 
	 * @return
	 * @author zhangqiang 2012-10-11上午10:55:39
	 */
	public String createApp() {
		AppApiResultDto appApiResultDto = new AppApiResultDto();
		resultlist = new ArrayList<AppApiResultDto>();
		if (appApiDto == null || appApiDto.getPtId() == null) {
			appApiResultDto = new AppApiResultDto();

			appApiResultDto.setStatus(0);
			appApiResultDto.setResultmsg("请输入数据");

			resultlist.add(appApiResultDto);
			return ERROR;
		}

		// 通过合作ID 查找密钥
		String keyPwd = createAppService.getKeyById(appApiDto.getPtId());

		if (keyPwd == null || keyPwd == "") {

			appApiResultDto = new AppApiResultDto();

			appApiResultDto.setStatus(0);
			appApiResultDto.setResultmsg("不存在合作商");

			resultlist.add(appApiResultDto);
			return ERROR;
		}
		// 通过id，密钥，时间戳 生成md5
		String md5 = CacheUtil.generatePassword(appApiDto.getPtId() + keyPwd
				+ appApiDto.getTime());
		// 匹配用户的md5
		if (!appApiDto.getPassword().equalsIgnoreCase(md5)) {

			appApiResultDto = new AppApiResultDto();

			appApiResultDto.setStatus(0);
			appApiResultDto.setResultmsg("您提供的密钥不正确");

			resultlist.add(appApiResultDto);
			return ERROR;
		}

		// 匹配成功
		appApiResultDto = createAppService.saveCeateApp(appApiDto,
				getWebAppsRealPath());

		resultlist.add(appApiResultDto);
		return SUCCESS;
	}

	/**
	 * 自动创建app
	 * 
	 * @return
	 * @author zhangqiang 2012-10-11上午10:55:39
	 */
	public String createAppForJson() {
		JSONArray jo = JSONArray.fromObject(jsonData);
		resultlist = new ArrayList<AppApiResultDto>();
		for (Object str : jo) {
			appApiDto = (AppApiDto) JSONObject.toBean(
					JSONObject.fromObject(str), AppApiDto.class);
			// 通过合作ID 查找密钥
			String keyPwd = createAppService.getKeyById(appApiDto.getPtId());
			AppApiResultDto appApiResultDto = new AppApiResultDto();
			if (keyPwd == null || keyPwd == "") {

				appApiResultDto = new AppApiResultDto();

				appApiResultDto.setStatus(0);
				appApiResultDto.setResultmsg("不存在合作商ID：" + appApiDto.getPtId());
				resultlist.add(appApiResultDto);
				continue;
				// return ERROR;
			}
			// 通过id，密钥，时间戳 生成md5
			 String md5 = CacheUtil.generatePassword(appApiDto.getPtId() +
			 keyPwd + appApiDto.getTime());
			// 匹配用户的md5
			if (!appApiDto.getPassword().equalsIgnoreCase(md5)) {

				appApiResultDto = new AppApiResultDto();

				appApiResultDto.setStatus(0);
				appApiResultDto.setResultmsg("合作商ID为：" + appApiDto.getPtId()
						+ " 您提供的密钥不正确");
				resultlist.add(appApiResultDto);
				continue;
				// return ERROR;
			}

			// 匹配成功
			appApiResultDto = createAppService.saveCeateApp(appApiDto,
					getWebAppsRealPath());
			resultlist.add(appApiResultDto);
		}

		return SUCCESS;
	}

	public String goMakeApp() {
		appApiDto = null;
		resultlist = null;
		return SUCCESS;
	}

	public AppApiDto getAppApiDto() {
		return appApiDto;
	}

	public void setAppApiDto(AppApiDto appApiDto) {
		this.appApiDto = appApiDto;
	}

	public List<AppApiResultDto> getResultlist() {
		return resultlist;
	}

	public void setResultlist(List<AppApiResultDto> resultlist) {
		this.resultlist = resultlist;
	}

	public String getJsonData() {
		return jsonData;
	}

	public void setJsonData(String jsonData) {
		this.jsonData = jsonData;
	}

}
